package com.maserb.monitor.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class DeviceTablesCreator {
    public void create(String jdbcDriver, String url, String username, String password, String dbname) {
        Connection conn = null;
        Statement statement = null;
        try {
            String[] sqls = new String[]{getDeviceTreeSql(), getDeviceTableSql(), getPartTableSql(), getDynamicPointTableSql(), getStaticPointTableSql(), getPointEigenValueTableSql()};
            Class.forName(jdbcDriver);
            conn = DriverManager.getConnection(url + dbname, username, password);
            statement = conn.createStatement();
            for (String sql : sqls) {
                statement.executeUpdate(sql);
            }
        } catch (Exception se) {
            se.printStackTrace();
        } finally {
            try {
                if (statement != null)
                    statement.close();
            } catch (SQLException se2) {
                try {
                    if (conn != null)
                        conn.close();
                } catch (SQLException se) {
                }
            }
        }

    }

    private String getDeviceTreeSql() {
        return "CREATE TABLE `device_tree` (\n" +
                "  `id` char(32) NOT NULL,\n" +
                "  `parentId` char(32) DEFAULT NULL,\n" +
                "  `name` varchar(255) DEFAULT NULL,\n" +
                "  `code` varchar(255) DEFAULT NULL,\n" +
                "  `category` varchar(255) DEFAULT NULL,\n" +
                "  `nodeType` smallint(6) DEFAULT NULL,\n" +
                "  `description` varchar(255) DEFAULT NULL,\n" +
                "  `createTime` varchar(255) DEFAULT NULL,\n" +
                "  PRIMARY KEY (`id`)\n" +
                ") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
    }

    private String getDeviceTableSql() {
        return "CREATE TABLE `device` (\n" +
                "  `id` char(32) NOT NULL,\n" +
                "  `code` varchar(255) DEFAULT NULL,\n" +
                "  `custom` varchar(255) DEFAULT NULL,\n" +
                "  `category` varchar(255) DEFAULT NULL,\n" +
                "  `createTime` varchar(255) DEFAULT NULL,\n" +
                "  `description` varchar(255) DEFAULT NULL,\n" +
                "  `image1` varchar(255) DEFAULT NULL,\n" +
                "  `image2` varchar(255) DEFAULT NULL,\n" +
                "  `image3` varchar(255) DEFAULT NULL,\n" +
                "  `image4` varchar(255) DEFAULT NULL,\n" +
                "  `latitude` varchar(255) DEFAULT NULL,\n" +
                "  `longitude` varchar(255) DEFAULT NULL,\n" +
                "  `name` varchar(255) DEFAULT NULL,\n" +
                "  PRIMARY KEY (`id`)\n" +
                ") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
    }

    private String getPartTableSql() {
        return "CREATE TABLE `part` (\n" +
                "  `id` char(32) NOT NULL,\n" +
                "  `custom` varchar(255) DEFAULT NULL,\n" +
                "  `code` varchar(255) DEFAULT NULL,\n" +
                "  `category` varchar(255) DEFAULT NULL,\n" +
                "  `createTime` varchar(255) DEFAULT NULL,\n" +
                "  `description` varchar(255) DEFAULT NULL,\n" +
                "  `image1` varchar(255) DEFAULT NULL,\n" +
                "  `image2` varchar(255) DEFAULT NULL,\n" +
                "  `image3` varchar(255) DEFAULT NULL,\n" +
                "  `image4` varchar(255) DEFAULT NULL,\n" +
                "  `latitude` varchar(255) DEFAULT NULL,\n" +
                "  `longitude` varchar(255) DEFAULT NULL,\n" +
                "  `name` varchar(255) DEFAULT NULL,\n" +
                "  PRIMARY KEY (`id`)\n" +
                ") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
    }

    private String getDynamicPointTableSql() {
        return "CREATE TABLE `dynamic_point` (\n" +
                "  `id` char(32) NOT NULL,\n" +
                "  `code` varchar(255) DEFAULT NULL,\n" +
                "  `custom` varchar(255) DEFAULT NULL,\n" +
                "  `createTime` varchar(255) DEFAULT NULL,\n" +
                "  `category` varchar(255) DEFAULT NULL,\n" +
                "  `description` varchar(255) DEFAULT NULL,\n" +
                "  `image1` varchar(255) DEFAULT NULL,\n" +
                "  `image2` varchar(255) DEFAULT NULL,\n" +
                "  `image3` varchar(255) DEFAULT NULL,\n" +
                "  `image4` varchar(255) DEFAULT NULL,\n" +
                "  `relationCode` varchar(255) DEFAULT NULL,\n" +
                "  `latitude` varchar(255) DEFAULT NULL,\n" +
                "  `longitude` varchar(255) DEFAULT NULL,\n" +
                "  `name` varchar(255) DEFAULT NULL,\n" +
                "  `unit` smallint DEFAULT NULL,\n" +
                "  `frequency` smallint DEFAULT NULL,\n" +
                "  `dataCount` smallint DEFAULT NULL,\n" +
                "  `sampleType` smallint DEFAULT NULL,\n" +
                "  `samplePeriod` double DEFAULT NULL,\n" +
                "  `sampleInterval` double DEFAULT NULL,\n" +
                "  `triggerLevel` double DEFAULT NULL,\n" +
                "  `triggerDelay` double DEFAULT NULL,\n" +
                "  PRIMARY KEY (`id`)\n" +
                ") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
    }

    private String getStaticPointTableSql() {
        return "CREATE TABLE `static_point` (\n" +
                "  `id` char(32) NOT NULL,\n" +
                "  `code` varchar(255) DEFAULT NULL,\n" +
                "  `relationCode` varchar(255) DEFAULT NULL,\n" +
                "  `custom` varchar(255) DEFAULT NULL,\n" +
                "  `createTime` varchar(255) DEFAULT NULL,\n" +
                "  `category` varchar(255) DEFAULT NULL,\n" +
                "  `description` varchar(255) DEFAULT NULL,\n" +
                "  `image1` varchar(255) DEFAULT NULL,\n" +
                "  `image2` varchar(255) DEFAULT NULL,\n" +
                "  `image3` varchar(255) DEFAULT NULL,\n" +
                "  `image4` varchar(255) DEFAULT NULL,\n" +
                "  `latitude` varchar(255) DEFAULT NULL,\n" +
                "  `longitude` varchar(255) DEFAULT NULL,\n" +
                "  `name` varchar(255) DEFAULT NULL,\n" +
                "  `sampleInterval` double DEFAULT NULL,\n" +
                "  PRIMARY KEY (`id`)\n" +
                ") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
    }

    private String getPointEigenValueTableSql() {
        return "CREATE TABLE `point_eigenvalue` (\n" +
                "  `id` char(32) NOT NULL,\n" +
                "  `code` varchar(255) DEFAULT NULL,\n" +
                "  `alarmType` Integer DEFAULT NULL,\n" +
                "  `baseline` Double DEFAULT NULL,\n" +
                "  `pointId` varchar(255) DEFAULT NULL,\n" +
                "  `warning1` Double DEFAULT NULL,\n" +
                "  `warning2` Double DEFAULT NULL,\n" +
                "  `danger1` Double DEFAULT NULL,\n" +
                "  `danger2` Double DEFAULT NULL,\n" +
                "  PRIMARY KEY (`id`)\n" +
                ") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
    }
}
